package service.implement;

import dao.AnimeDao;
import dao.RatingDao;
import dao.UserDao;
import dao.impl.AnimeDaoImplement;
import dao.impl.RatingDaoImplement;
import dao.impl.UserDaoImplement;
import pojo.Anime;
import pojo.Ratingrecord;
import pojo.User;
import service.UserService;

import java.util.List;

public class UserServiceImplement implements UserService {
    UserDao userDao=new UserDaoImplement();
    AnimeDao animeDao=new AnimeDaoImplement();
    RatingDao ratingDao=new RatingDaoImplement();

    @Override
    public String Provideregister() {
        //获取到最大的id
      String max_id=userDao.queryMaxID().toString();
      //将id+1即为当前可用id
      int  curId_Int=(Integer.parseInt(max_id)+1);
      String curId_string=Integer.toString(curId_Int);
      userDao.saveUser(new User(curId_string));
      return curId_string;
    }


    @Override
    public Boolean isExistId(String user_ID) {


        // 判断用户ID 输入合法
        Boolean flag=false;
        String reg="[1-9][0-9]*";
        flag=user_ID.matches(reg);

        if(flag && (userDao.queryUserbyId(user_ID)!=null)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<Anime> searchAnimeList(String userInput, String category) {
        System.out.println("userService 正在处理"+userInput+""+category);
     switch (category){
         case "name":
            return animeDao.queryAnimebyName(userInput);
         case "type":
             return animeDao.queryAnimebyType(userInput);
         case "genre":
             return animeDao.queryAnimebyGenre(userInput);
         default:
             return null;

     }
    }


    @Override
    public String rate(String user_ID, String anime_ID, int rat) {

        String result=ratingDao.doRating(user_ID,anime_ID,rat);

        if(result.equals("404")){
            return "对不起，系统不存在该id的动漫";
        }else{
            Anime anime=animeDao.queryAnimebyId(anime_ID);
            String name=anime.getName();
            return "谢谢您为id:"+anime_ID+" 名字:"+name+"动漫成功评分";
        }
    }

    @Override
    public List<Anime> recommendationList(String user_id) {
        return userDao.doRecommendation(user_id);
    }

    @Override
    public  List<Ratingrecord> recordHistory(String user_id){
             return userDao.searchHistory(user_id);
    }
}
